import java.util.PriorityQueue;

/**
 * 703. 数据流中的第 K 大元素
 * https://leetcode.cn/problems/kth-largest-element-in-a-stream/description/
 */
class KthLargest {
    PriorityQueue<Integer> heap;
    int k;
    public KthLargest(int _k, int[] nums) {
        k = _k;
        heap = new PriorityQueue<>();
        for(int x : nums) {
            heap.offer(x);
            if(heap.size() > k) heap.poll();
        }
    }

    public int add(int val) {
        heap.offer(val);
        if(heap.size() > k) heap.poll();
        return heap.peek();
    }
}
